﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Persistency.DataAccess;
using BusinessObjects.Objects;
using System.Collections;


namespace Persistency.DAL
{
	public static class OrganisatonDAL
	{
		
		#region Members

		private static string GetAllOrganisationStatement = @"dbo.GetOrganisation";
		private const string UpdateOrganisationProcedure = @"dbo.SaveOrganisation";

		#endregion

		#region Methods

		public static void SaveOrganisation(OrganisationObject organisationObject)
		{
			IDictionary<string, object> parameters = GetParameters(organisationObject);
			SQLHelper.ExecuteStoredProcedure(UpdateOrganisationProcedure, parameters);
		}

		#endregion

		#region Functions

		private static OrganisationObject ConvertToOrganisation(DataRow row)
		{
			OrganisationObject organisation = new OrganisationObject();
			organisation.Adresse = (string)row["Adresse"];
            organisation.eMail = (string)row["EMail"];
            organisation.EPLZ = (Int32)row["EPLZ"];
            organisation.Export = (bool)row["Export"];
            organisation.FaxNr = (string)row["FaxNr"];
            organisation.Kurzwahlspeicher = (string)row["Kurzwahlspeicher"];
            organisation.Notiz = (string)row["Notiz"];
            organisation.OrgKey = (Int32)row["OrgKey"];
            organisation.OrgName = (string)row["OrgName"];
			organisation.Plz = (string)row["Plz"];
            organisation.Prefix = (string)row["Prefix"];
			organisation.Straße = (string)row["Straße"];
            organisation.Suchbegriff = (string)row["Suchbegriff"];
            organisation.TelNr1 = (string)row["TelNr1"];
            organisation.TelNr2 = (string)row["TelNr2"];
            organisation.ZusätzeFW = (string)row["ZusätzeFW"];
            
			return organisation;
		}

		private static List<OrganisationObject> ConvertToOrganisationen(DataTable dataTable)
		{
			List<OrganisationObject> organisationen = new List<OrganisationObject>();

			foreach (DataRow row in dataTable.Rows)
			{
				organisationen.Add(ConvertToOrganisation(row));
			}
			return organisationen;
		}

		public static List<OrganisationObject> GetOrganisation()
		{
			return GetOrganisation(0);
		}

		public static List<OrganisationObject> GetOrganisation(int orgKey)
		{
			IDictionary<string, object> parameters = new Dictionary<string, object>();
			parameters.Add("@inOrgKey", orgKey);
			DataTable dataTable = SQLHelper.ExecuteDataTable(GetAllOrganisationStatement, parameters);
			return ConvertToOrganisationen(dataTable);
		}

		private static IDictionary<string, object> GetParameters(OrganisationObject org)
		{
			IDictionary<string, object> parameters = new Dictionary<string, object>();
			parameters.Add("@inOrgKey", org.OrgKey);
			parameters.Add("@inOrgName", org.OrgName);
            parameters.Add("@inOrgTS", ""); 
            parameters.Add("@inPrefix", org.Prefix);
            parameters.Add("@inZusätzeFW", org.ZusätzeFW); 
            parameters.Add("@inRechtsFKey", ""); 
            parameters.Add("@inSuchbegriff", org.Suchbegriff);
            parameters.Add("@inAbteilung", org.Abteilung);
            parameters.Add("@inAdresse", org.Adresse);
            parameters.Add("@inEPLZ", org.EPLZ);
            parameters.Add("@inStraße", org.Straße);
            parameters.Add("@inTelNr1", org.TelNr1);
            parameters.Add("@inTelNr2", org.TelNr2);
            parameters.Add("@inFaxNr", org.FaxNr);
            parameters.Add("@inEMail", org.eMail);
            parameters.Add("@inKurzwahlspeicher", org.Kurzwahlspeicher);
            parameters.Add("@inNotiz", org.Notiz);
            parameters.Add("@inExport", org.Export);

			return parameters;
		}

		#endregion


	}
}
